<?php

namespace app\controller;

use app\Request;
use app\service\DepartmentManagerService;

class DepartmentManagerController
{

    public function getList(Request $request)
    {
        $departmentId = $request->post("department_id");
        $leaderName = $request->post("leader_name");
        $leaderPhone = $request->post("leader_phone");
        $leaderType = $request->post("leader_type");
        $page = $request->post("page");
        $pageSize = $request->post("pageSize");

        $result = DepartmentManagerService::getInstance()->getDepartmentManagerList($departmentId, $leaderName, $leaderPhone, $leaderType, $page, $pageSize);
        return jsonResponse($result);
    }

    public function create(Request $request)
    {
        validate([
            "department_id" => "require",
            "leader_name" => "require",
            "leader_phone" => "require",
            "leader_type" => "require"
        ])->check($request->post());

        $departmentId = $request->post("department_id");
        $leaderName = $request->post("leader_name");
        $leaderPhone = $request->post("leader_phone");
        $leaderType = $request->post("leader_type");

        $result = DepartmentManagerService::getInstance()->createDepartmentManager($departmentId, $leaderName, $leaderPhone, $leaderType);
        return jsonResponse($result);
    }

    public function update(Request $request)
    {
        validate([
            "id" => "require",
            "department_id" => "require",
            "leader_name" => "require",
            "leader_phone" => "require",
            "leader_type" => "require"
        ])->check($request->post());

        $id = $request->post("id");
        $departmentId = $request->post("department_id");
        $leaderName = $request->post("leader_name");
        $leaderPhone = $request->post("leader_phone");
        $leaderType = $request->post("leader_type");

        $result = DepartmentManagerService::getInstance()->updateDepartmentManager($id, $departmentId, $leaderName, $leaderPhone, $leaderType);
        return jsonResponse($result);
    }

    public function delete(Request $request)
    {
        validate([
            "id" => "require"
        ])->check($request->post());

        $id = $request->post("id");

        $result = DepartmentManagerService::getInstance()->deleteDepartmentManager($id);
        return jsonResponse($result);
    }
}